package boun.center;

import java.io.FileInputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class RegistrationActivity extends Activity {
	GridView grid;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.final_exam_layout);
		grid = (GridView) findViewById(R.id.final_grid);
		Button b = (Button) findViewById(R.id.RefreshButton);
		b.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				new FetchingTask().execute();
			}
			
		});
		new FetchingTask().execute();
	}
	
	class FetchingTask extends AsyncTask<Void,Void,FinalExamListAdapter>{
		ProgressDialog dialog;
		public FetchingTask(){
			dialog = new ProgressDialog(RegistrationActivity.this);
		}
		@Override
		protected FinalExamListAdapter doInBackground(Void... params) {
			FinalExamListAdapter ca;
			try {
				System.out.println("iceride");
				FileInputStream fis = openFileInput("preferences");
				Pair<String,String> p = readFrom(fis);
				System.out.println("disarida" + p.first +"------------"+ p.second);
				if(p.first.equals("") || p.second.equals("")){
					System.out.println("GRID OGUZZZZZZZZZZZZZZZZZZZZZZZ ");
					LinearLayout ll = new LinearLayout(RegistrationActivity.this);
					ll.setLayoutParams(new   ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
					TextView tv = new TextView(RegistrationActivity.this);
					tv.setText("You have to set a username and password from Pref screen in order to view your final dates.");
					ll.addView(tv);
					grid.addView(ll);
					return null;
				}else{
					ca = new FinalExamListAdapter(RegistrationActivity.this,p);
					return ca;
				}
				
			} catch (IOException e) {
			} catch (Exception e) {
			}
			return null;
		}
		
		@Override
		protected void onPreExecute(){
			dialog = ProgressDialog.show(RegistrationActivity.this,"","Fetching..",true);
		}
		@Override
		protected void onPostExecute(FinalExamListAdapter la){
			dialog.dismiss();
			grid.setAdapter(la);
		}
	}
	
	public static Pair<String, String> readFrom(FileInputStream fis) throws IOException {
		byte[] buffer = new byte[1024];
		fis.read(buffer);
		fis.close();
		String m = new String(buffer);
		String id = m.split("\n")[0];
		String pw = m.split("\n")[1];
		return new Pair<String,String>(id,pw);
	}
}
